package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import net.sf.json.JSONSerializer; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.List;

import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhi;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiService;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKy;
import com.lb.jwitter.service.TblQLDTTXAHocVienTamDungHocKyService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;

import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;

import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienPhieuDangKyMonHocInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }
    
    private Collection<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public Collection<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private String ngayNop; 
    public void setNgayNop(String ngayNop) {
       this.ngayNop = ngayNop;
 }

    public String getNgayNop() {
       return ngayNop;
 }

    private int canBoNhanId; 
    public void setCanBoNhanId(int canBoNhanId) {
       this.canBoNhanId = canBoNhanId;
 }

    public int getCanBoNhanId() {
       return canBoNhanId;
 }

    private Collection<TblCanBo> tblCanBos; 
    public Collection<TblCanBo>  getTblCanBos(){ 
              return tblCanBos; 
     } 

    private TblCanBoService tblCanBoService; 

    private int hocKyId; 
    public void setHocKyId(int hocKyId) {
       this.hocKyId = hocKyId;
 }

    public int getHocKyId() {
       return hocKyId;
 }

    private Collection<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public Collection<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    
    private TblQLDTTXAHocVienHocPhiService tblQLDTTXAHocVienHocPhiService;

    private TblQLDTTXAHocVienPhieuDangKyMonHoc tblQLDTTXAHocVienPhieuDangKyMonHoc; 
    public TblQLDTTXAHocVienPhieuDangKyMonHoc  getTblQLDTTXAHocVienPhieuDangKyMonHoc() {
        return tblQLDTTXAHocVienPhieuDangKyMonHoc;
 }

    private Collection<TblQLDTTXAHocVienPhieuDangKyMonHoc> tblQLDTTXAHocVienPhieuDangKyMonHocs; 
    public Collection<TblQLDTTXAHocVienPhieuDangKyMonHoc>  getTblQLDTTXAHocVienPhieuDangKyMonHocs() {
         return tblQLDTTXAHocVienPhieuDangKyMonHocs; 
      } 

    private TblQLDTTXAHocVienPhieuDangKyMonHocService tblQLDTTXAHocVienPhieuDangKyMonHocService; 

    private String tblQLDTTXAHocVienDangKyMonHocStr; 
    public void setTblQLDTTXAHocVienDangKyMonHocStr(String tblQLDTTXAHocVienDangKyMonHocStr) 
{
       this.tblQLDTTXAHocVienDangKyMonHocStr = tblQLDTTXAHocVienDangKyMonHocStr;
 }

    public String getTblQLDTTXAHocVienDangKyMonHocStr() 
 {
       return tblQLDTTXAHocVienDangKyMonHocStr;
 }

    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService; 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 
    
    private TblQLDTTXAHocVienTamDungHocKyService tblQLDTTXAHocVienTamDungHocKyService;

	private int preInsert(Message msg, TblQLDTTXAHocVienPhieuDangKyMonHoc obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXAHocVienPhieuDangKyMonHoc obj ){
		try{
			
//			TblQLDTTXAHocVienHocPhi tblQLDTTXAHocVienHocPhi = new TblQLDTTXAHocVienHocPhi();
//			tblQLDTTXAHocVienHocPhi.setHocKyId(obj.getHocKyId());
//			tblQLDTTXAHocVienHocPhi.setHocPhi(new Double(0));
//			tblQLDTTXAHocVienHocPhi.setHocVienId(obj.getHocVienId());
//			tblQLDTTXAHocVienHocPhi.setSoMonHoc(0);
//			tblQLDTTXAHocVienHocPhi.setSoTinChi(0);
//			tblQLDTTXAHocVienHocPhi.setTienHaTang(0.0);
//			tblQLDTTXAHocVienHocPhi.setTienPhaiDong(0.0);
//			tblQLDTTXAHocVienHocPhi.setTrangThai(false);
//			tblQLDTTXAHocVienHocPhiService.createtblQLDTTXAHocVienHocPhi(tblQLDTTXAHocVienHocPhi);

	        msg.setPostMessage_b(true); 
	        msg.setPostMessage_s("success"); 
	        return 1; 
		} catch (Exception e) {
			
	        msg.setPostMessage_b(false); 
	        msg.setPostMessage_s("LÃ¯Â¿Â½Ã¯Â¿Â½Ã¯Â¿Â½Ã¯Â¿Â½i khi thÃ¯Â¿Â½Ã¯Â¿Â½m phiÃ¯Â¿Â½Ã¯Â¿Â½Ã¯Â¿Â½Ã¯Â¿Â½u Ã¯Â¿Â½Ã¯Â¿Â½Ã¯Â¿Â½Ã¯Â¿Â½ng kyÃ¯Â¿Â½Ã¯Â¿Â½ mÃ¯Â¿Â½Ã¯Â¿Â½n hoÃ¯Â¿Â½Ã¯Â¿Â½c"); 
	        return 1; 
		}
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc", "them")){ 
        return "noprivilege"; 
    }        tblQLDTTXAHocVienPhieuDangKyMonHocService = (TblQLDTTXAHocVienPhieuDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_PhieuDangKyMonHocClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient"); 

        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient");
        
        tblQLDTTXAHocVienTamDungHocKyService = (TblQLDTTXAHocVienTamDungHocKyService)  context.getBean("tbl_QLDTTXA_HocVien_TamDung_HocKyClient");
        msg = new Message(); 
        
        TblQLDTTXAHocVien hocVien = tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId));
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        cons.clear();
        {         	
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(new Long(hocKyId));
            con.setOperator("eq");
            cons.add(con);	
        }
        {         	
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(hocVien.getId());
            con.setOperator("eq");
            cons.add(con);	
        }
        List<TblQLDTTXAHocVienTamDungHocKy> tblQLDTTXAHocVienTamDungHocKys = tblQLDTTXAHocVienTamDungHocKyService.findtblQLDTTXAHocVienTamDungHocKyByCondition(cons, 0, 0);
        if (tblQLDTTXAHocVienTamDungHocKys.size() > 0){
        	msg.setPreMessage_b(false); 
            msg.setPreMessage_s("Tình trạng học viên : tạm dừng học kỳ"); 
            
        	return Action.ERROR;
        }
        
        if (!hocVien.getTrangThaiId().getId().equals(new Long(1))){
        	msg.setPreMessage_b(false); 
            msg.setPreMessage_s("Tình trạng học viên : " + hocVien.getTrangThaiId().getTen()); 
            
        	return Action.ERROR;
        }
        
        //List<ConditionReport> cons = new ArrayList<ConditionReport>();
        cons.clear();
        if(hocKyId != 0){         	
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(new Long(hocKyId));
            con.setOperator("eq");
            cons.add(con);	
        }
        if(hocVienId != 0){         	
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(new Long(hocVienId));
            con.setOperator("eq");
            cons.add(con);	
        }
        List<TblQLDTTXAHocVienPhieuDangKyMonHoc> phieuDangKys = tblQLDTTXAHocVienPhieuDangKyMonHocService.findtblQLDTTXAHocVienPhieuDangKyMonHocByCondition(cons, 0, 0);
        if(phieuDangKys != null && phieuDangKys.size() > 0){
        	msg.setPreMessage_b(false); 
            msg.setPreMessage_s("Phiếu đăng ký môn học đã tồn tại."); 
            
        	return Action.ERROR;
        	
        } else {        	
        
	        tblQLDTTXAHocVienPhieuDangKyMonHoc = new TblQLDTTXAHocVienPhieuDangKyMonHoc(); 
	        tblQLDTTXAHocVienPhieuDangKyMonHoc.setHocVienId(hocVien); 
	       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
	       Date dd = df.parse(ngayNop); 
	       GregorianCalendar c = new GregorianCalendar(); 
	       c.setTime(dd);
	       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
	        tblQLDTTXAHocVienPhieuDangKyMonHoc.setNgayNop(d); 
	        tblQLDTTXAHocVienPhieuDangKyMonHoc.setCanBoNhanId(tblCanBoService.gettblCanBo(new Long(canBoNhanId))); 
	        tblQLDTTXAHocVienPhieuDangKyMonHoc.setHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(hocKyId))); 
	        preInsert(msg,tblQLDTTXAHocVienPhieuDangKyMonHoc); 
	       if (msg.isPreMessage_b()) 
	       { 
	           id = tblQLDTTXAHocVienPhieuDangKyMonHocService.createtblQLDTTXAHocVienPhieuDangKyMonHoc(tblQLDTTXAHocVienPhieuDangKyMonHoc);
	           tblQLDTTXAHocVienPhieuDangKyMonHoc.setId(id); 
	           
	           
	           postInsert(msg, tblQLDTTXAHocVienPhieuDangKyMonHoc); 
	           if (msg.isPostMessage_b() == false) 
	           { 
	               return Action.ERROR; 
	           } 
	       }else{ 
	           return Action.ERROR; 
	       } 
	       
	       
			JSONArray jsonTblQLDTTXAHocVienDangKyMonHoc = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienDangKyMonHocStr); 
			for (int i = 0; i < jsonTblQLDTTXAHocVienDangKyMonHoc.size(); i++) 
			 { 
			      JSONObject jsObj = jsonTblQLDTTXAHocVienDangKyMonHoc.getJSONObject(i); 
			        TblQLDTTXAHocVienDangKyMonHoc obj0 = new TblQLDTTXAHocVienDangKyMonHoc(); 
			        obj0.setMonHocId(tblQLDTTXAMonHocService.gettblQLDTTXAMonHoc(new Long(jsObj.getInt("monHocId")))); 
			       obj0.setPhieuDangKyId(tblQLDTTXAHocVienPhieuDangKyMonHoc); 
			       tblQLDTTXAHocVienDangKyMonHocService.createtblQLDTTXAHocVienDangKyMonHoc(obj0); 
			   } 
	        return Action.SUCCESS; 
	
	        }
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
